// pages/caseReward/entrustDet/entrustDet.js
import request from '../../../utils/config.js';
import formatUtil from '../../../utils/util.js';
import cityData from '../../../utils/vendor/cityList.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userExt: {},
    memUserInfo: {},
    entrustId:"",
    entrustDet:{},
    showPerInfo:false,//个人信息显示
    // btnMsg:"承接悬赏"
  },

  //下拉执行
  onPullDownRefresh: function () {
    this.queryEntrustDet();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      entrustId: options.entrustId,
      memUserInfo: app.globalData.persionalInfo
    });
    this.queryEntrustDet();
  },

  //查询委托尽调详情
  queryEntrustDet: function () {
    let headerInfo = {
      sessionId: app.globalData.sessionId,
    }
    let bodyInfo = {
      entrustId: this.data.entrustId*1 || ""
    }
    let _this = this;
    wx.showLoading({ title: '加载中', mask: true });
    request.requestLogin_api("queryEntrustConsult", headerInfo, bodyInfo, (obj) => {
      wx.stopPullDownRefresh();
      console.log("详情=",obj)
      wx.hideLoading();
      if (obj.data.errorCode == '00') {
        let entrustDet = JSON.parse(obj.data.body);
        entrustDet.entrustName = formatUtil.getEntrustNameById(entrustDet.entrustType + '');
        entrustDet.operName = entrustDet.operName.substring(0,1)+"律师";
        entrustDet.endDate = formatUtil.checkYearMonthDay(entrustDet.endDate);
        _this.setData({
          entrustDet: entrustDet
        });
      } else {
        wx.showModal({
          title: "查询失败",
          content: obj.data.errorMsg,
          confirmText: '确定',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            } else if (res.cancel) {
            }
          }
        })
      }
      
    }, (obj) => {
      wx.stopPullDownRefresh();
      wx.hideLoading();
      wx.showModal({
        title: "加载失败",
        content: '请尝试下拉界面重新加载',
        confirmText: '确定',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    })
  },

  //点击开始确认信息
  confirmInfo: function(){
    if (!this.data.showPerInfo){
        this.setData({
          showPerInfo:true
        });
        return;
    }
  },

  //开始承接悬赏
  startAccecpt: function(){
    //判断是否是律师
    if (this.data.userExt.userType != '1'){
      wx.showModal({
        title: "您当前还不是律师用户",
        content: "是否注册为律师",
        confirmText: '确定',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../lawyerConfirm/lawyerConfirm',
            })
          } else if (res.cancel) {

          }
        }
      })
      return;
    }



    let headerInfo = {
      sessionId: app.globalData.sessionId,
    }
    let bodyInfo = {
      entrustId: this.data.entrustId * 1,
      // lawyerUserId: this.data.memUserInfo.userId*1,
      email: this.data.userExt.email,
      mobileNo: this.data.userExt.mobileNo,
      userName: this.data.userExt.userName
    }
    console.log("bodyInfo=", bodyInfo)
    let _this = this;
    wx.showLoading({ title: '加载中', mask: true });
    request.requestLogin_api("entrustConsultApply", headerInfo, bodyInfo, (obj) => {
      wx.stopPullDownRefresh();
      console.log("委托申请=", obj)
      wx.hideLoading(obj);
      if (obj.data.errorCode == '00'){
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateTo({
              url: '../myReward/myReward',
          })
        }, 1000);
        // wx.showModal({
        //   title: "申请成功",
        //   content: "是否查看我的申请列表",
        //   confirmText: '确定',
        //   showCancel: false,
        //   success: function (res) {
        //     if (res.confirm) {
        //       wx.navigateTo({
        //         url: '../myReward/myReward',
        //       })
        //     } else if (res.cancel) {

        //     }
        //   }
        // })
      }else{
        wx.showModal({
          title: "申请失败",
          content: obj.data.errorMsg,
          confirmText: '确定',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            } else if (res.cancel) {
            }
          }
        })
      }
     
    }, (obj) => {
      wx.stopPullDownRefresh();
      wx.hideLoading();
      wx.showModal({
        title: "加载失败",
        content: '请尝试下拉界面重新加载',
        confirmText: '确定',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userExt: app.globalData.userExt
    });
  },

  //跟人信息页面
  goPersonalInfo: function(){
    wx.navigateTo({
      url: '../personalInfo/personalInfo',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log("来自页面内的转发", res.target)
    }
    let This = this;
    let userName = app.globalData.userName ? app.globalData.userName : app.globalData.userInfo.nickName
    return {
      title: userName + "邀请您承接“" + this.data.entrustDet.entrustName +"”的委托查档,速去领取！",
      path: 'pages/index/index?entrustId=' + this.data.entrustId,
      imageUrl: '../img/photo.png',
      success: function (res) {
        // 转发成功
        console.log("转发成功返回数据=", res)
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });

        if (res.shareTickets) {
          // 获取转发详细信息
          wx.getShareInfo({
            shareTicket: res.shareTickets[0],
            success(res) {
             
            },
            fail() { },
            complete() { }
          });
        }
      },
      fail: function (res) {
        // 转发失败
      },
      complete: function () {

      }
    }
  }
})